var myCart = []
Page({
  data:{
    num:0,
    sumPrice:0,
    sumNum:0,
    foodlist:[],
    cartList:[]
  },
  onLoad(e)
  {
    let myCart = wx.getStorageSync('mycart')
    console.log(e.id)
    wx.cloud.callFunction({
      name:'shopid',
      data:{
        pid:Number(e.id)
      }
    })
    .then(res=>{
      this.setData({
        img:res.result.data[0].img
      })
    })
    .catch(err=>{
      console.error(err)
    })
    if(myCart)
    {
      console.log(1)
      wx.cloud.callFunction({
        name:'goods',
        data:{
          pid:e.id
        }
      })
      .then(res=>{
        console.log(res);
        let list = res.result.data
        let showprice = 0,showsum = 0
        if(list && list.length > 0)
        {
          list.forEach(item=>{
            item.num = 0
            myCart.forEach(item1=>{
              if(item._id == item1._id)
              {
                item.num = item1.num
              }
            })
            showprice += item.price*item.num
            showsum += item.num
          })

          this.data.foodlist = list
          this.setData({
            listid:res.result.data,
            sumPrice:showprice,
            sumNum:showsum,
          })
        }
      })
      .catch(err=>{
        console.error(err);
      })
    }
    else
    {
      console.log(0)
      wx.cloud.callFunction({
        name:'goods',
        data:{
          pid:e.id
        }
      })
      .then(res=>{
        console.log(res);
        let list = res.result.data
        let showprice = 0,showsum = 0
        if(list && list.length > 0)
        {
          list.forEach(item=>{
            item.num = 0
            showprice += item.price*item.num
            showsum += item.num
          })

          this.data.foodlist = list
          this.setData({
            listid:res.result.data,
            sumPrice:showprice,
            sumNum:showsum,
          })
        }
      })
      .catch(err=>{
        console.error(err);
      })
    }
  },
  add(e)
  {
    let id = e.currentTarget.dataset.id
    let list = this.data.foodlist
    let cartList = this.data.cartList
    console.log("前",list)
    list.forEach(item=>{
      if(item._id == id)
      {
        item.num += 1
        if(cartList && cartList.length > 0)
        {
          var res = cartList.find(cart => {
            return cart._id == id
          })
          console.log("当前点击的菜品是否存在",res)
          if(!res) 
          {
            cartList.push(item)
          }
        }
        else
        {
          cartList.push(item)
        }
        this.setData({
          sumPrice:this.data.sumPrice += item.price,
          sumNum:this.data.sumNum += 1,
          cartList:cartList
        })
      }
    })
    this.data.foodlist = list
    console.log('++++++++购物车列表',cartList)
    wx.setStorageSync('mycart', cartList)
    myCart = cartList
    this.setData({
      listid:list
    })
  },
  del(e)
  {
    let id = e.currentTarget.dataset.id
    let list = this.data.foodlist
    let cartList = this.data.cartList

    list.forEach(item=>{
      if(item._id == id)
      {
        if(item.num <= 0)
        {
          item.num = 0
        }
        else
        {
          item.num -= 1
          if(cartList && cartList.length > 0)
          {
            var res = cartList.find(cart => {
              return cart._id == id
            })
            if(res.num == 0) 
            {
              cartList.pop(res)
            }
          }
          console.log('-----购物车列表',cartList)
          this.data.sumPrice -= item.price
          var midSumPrice = this.data.sumPrice
          midSumPrice = Math.floor(midSumPrice * 100) / 100 
          if(midSumPrice < 0)
          {
            midSumPrice = 0
          }
          myCart = cartList
          this.setData({
            sumPrice:midSumPrice,
            sumNum:this.data.sumNum -= 1,
            cartList:cartList
          })
        }
      }
    })
    console.log(cartList)
    this.setData({
      listid:list
    })
  },
  // jumpcar(e)
  // {
  //   let idlist = []
  //   console.log(e.currentTarget.dataset.id)
  //   let list = e.currentTarget.dataset.id
  //   list.forEach(item=>{
  //     if(item.num > 0)
  //     {
  //       idlist.push(item._id)
  //     }
  //   })
  //   console.log('我是',myCart)

  //   wx.setStorageSync('mycart', myCart)
  //   wx.switchTab({
  //     url: '/pages/car/car',
  //   })
  // },

  jumpcar(e)
  {
    let idlist = []
    myCart = wx.getStorageSync('mycart')
    
    console.log(e.currentTarget.dataset.id)
    let list = e.currentTarget.dataset.id
    list.forEach(item=>{
      if(item.num > 0)
      {
        idlist.push(item)
      }
    })
    console.log('我是后idlist',idlist)
    wx.setStorageSync('mycart', idlist)
    wx.switchTab({
      url: '/pages/car/car',
    })
  }
})